package com.kantboot.system.user.service.impl;

import com.kantboot.system.user.dao.repository.entity.SysUserRoleRepository;
import com.kantboot.system.user.dao.repository.vo.SysUserOfAdminVORepository;
import com.kantboot.system.user.domain.dto.SysUserOfAdminSearchDTO;
import com.kantboot.system.user.domain.entity.SysUserRole;
import com.kantboot.system.user.domain.vo.SysUserOfAdminVO;
import com.kantboot.system.user.exception.SystemUserException;
import com.kantboot.system.user.service.ISysUserOfAdminVOService;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserOfAdminVOServiceImpl implements ISysUserOfAdminVOService {

    @Resource
    private SysUserOfAdminVORepository repository;

    @Resource
    private SysUserRoleRepository userRoleRepository;

    @Override
    public PageResult getBodyData(PageParam<SysUserOfAdminSearchDTO> pageParam) {
        List<String> roleCodes = pageParam.getData().getRoleCodes();
        if (roleCodes != null && roleCodes.isEmpty()) {
            pageParam.getData().setRoleCodes(null);
        }

        return PageResult.of(repository.getBodyData(pageParam.getData(),pageParam.getPageable()));
    }

    @Override
    public void addRole(Long userId, List<String> roleCodes) {
        // 如果用户不存在，抛出异常
        SysUserOfAdminVO sysUser = repository.findById(userId).orElseThrow(() -> SystemUserException.USER_NOT_EXIST);
        List<SysUserRole> roleList = sysUser.getUserRoleList();
        if (roleList == null) {
            roleList = new ArrayList<>();
        }
        List<String> roleCodeListOfAdd = new ArrayList<>();
        // 如果已经有了，就不添加了
        for (String roleCode : roleCodes) {
            boolean exist = false;
            for (SysUserRole sysUserRole : roleList) {
                if (sysUserRole.getRoleCode().equals(roleCode)) {
                    exist = true;
                    break;
                }
            }
            if (!exist) {
                roleCodeListOfAdd.add(roleCode);
            }
        }


        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (String roleCode : roleCodeListOfAdd) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleCode(roleCode);
            sysUserRoleList.add(sysUserRole);
        }
        userRoleRepository.saveAll(sysUserRoleList);
    }

    @Override
    public void removeRole(Long userId, List<String> roleCodes) {
        List<SysUserRole> roleList = userRoleRepository.findNotAdminByUserId(userId);
        List<SysUserRole> roleList2 = new ArrayList<>();
        for (SysUserRole sysUserRole : roleList) {
            for (String roleCode : roleCodes) {
                if (sysUserRole.getRoleCode().equals(roleCode)) {
                    roleList2.add(sysUserRole);
                }
            }
        }
        userRoleRepository.deleteAll(roleList2);
    }

    @Override
    public void setRole(Long userId, List<String> roleCodes) {
        List<SysUserRole> notAdminByUserId = userRoleRepository.findNotAdminByUserId(userId);
        List<SysUserRole> roleList = new ArrayList<>();
        for (SysUserRole sysUserRole : notAdminByUserId) {
            if(sysUserRole.getVisible()==null||sysUserRole.getVisible()){
                roleList.add(sysUserRole);
            }
        }

        // 删除所有角色，除了admin
        userRoleRepository.deleteAll(roleList);
        // 添加角色
        addRole(userId, roleCodes);
    }



}
